package com.woniu.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.Service.FlowerCommentService;
import com.woniu.entity.FlowerComment;
import com.woniu.mapper.FlowerCommentMapper;
import com.woniu.vo.FlowerCommentVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: Gwj
 * @Date: 2024/7/6 10:35
 */
@Service
@Transactional
public class FlowerCommentServiceImpl implements FlowerCommentService {
    @Resource
    private FlowerCommentMapper flowerCommentMapper;

    /**
     * 查询所有鲜花订单的评论，分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<FlowerCommentVo> findAllFlowerComments(int pageNum, int pageSize) throws Exception {
        Page<FlowerCommentVo> page = PageHelper.startPage(pageNum, pageSize);
        // 查询所有花评论
        flowerCommentMapper.findAllFlowerComments();
        return page.toPageInfo();
    }

    /**
     * 根据条件查询符合条件的鲜花评论 并且分页
     *
     * @param flowerCommentVo
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<FlowerCommentVo> findAllFlowerCommentsByConditions(FlowerCommentVo flowerCommentVo, int pageNum, int pageSize) throws Exception {
        Page<FlowerCommentVo> page = PageHelper.startPage(pageNum, pageSize);
        // 查询符合条件的花评论
        flowerCommentMapper.findAllFlowerCommentsByConditions(flowerCommentVo);
        return page.toPageInfo();
    }

    /**
     * 删除评论 逻辑删除
     *
     * @param flowerComment
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value="flowerComment" , allEntries = true)
    public int changeFlowerCommentIsDelete(FlowerComment flowerComment) throws Exception {
        flowerComment.setIsDelete((byte) 1);
        int i = flowerCommentMapper.updateByPrimaryKeySelective(flowerComment);
        return i;
    }

    /**
     * 恢复评论 把删除字段改为0
     *
     * @param flowerComment
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value="flowerComment" , allEntries = true)
    public int replyFlowerComment(FlowerComment flowerComment) throws Exception {
        flowerComment.setIsDelete((byte) 0);
        int i = flowerCommentMapper.updateByPrimaryKeySelective(flowerComment);
        return i;
    }
}
